<template>
  <div class="login-container" :style="{'background-image': `url(${require('@/assets/login-bg.png')})`}">
    <div class="login-content">
      <div class="login-content-left">
        <div class="login-qrcode">
          <div class="content-qr-text">扫码登录</div>
          <div class="content-qrcode">
            <img width="150px" height="150px" src="http://admin.api.moyangjia.top/static/picture/1690953937104-hgno.png" alt="">
          </div>
          <div class="content-text">
            <div>使用微信扫码登录</div>
            <div>扫一扫登录更轻松</div>
          </div>
        </div>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">账号登录</h3>
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input class="login-input" v-model="loginForm.username" name="username" type="text" tabindex="1" auto-complete="on" placeholder="用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input class="login-input" v-model="loginForm.password" :type="passwordType" name="password" auto-complete="on" tabindex="2" show-password placeholder="密码" @keyup.enter.native="handleLogin" />
        </el-form-item>

        <!-- <el-form-item prop="code">
          <span class="svg-container">
            <svg-icon icon-class="lock" />
          </span>
          <el-input v-model="loginForm.code" auto-complete="off" name="code" tabindex="2" placeholder="验证码" style="width: 60%" @keyup.enter.native="handleLogin" />
          <div class="login-code">
            <img :src="codeImg" @click="getCode">
          </div>
        </el-form-item> -->

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:15px;height: 44px;" @click.native.prevent="handleLogin">登录</el-button>
        <el-button :loading="loading" type="text" style="width:100%;height: 44px;" @click.native.prevent="showRegist=true">注册新用户</el-button>

        <div style="position:relative;display: none;">
          <div class="tips">
            <span> 超级管理员用户名: admin123</span>
            <span> 超级管理员用户名：admin123</span>
          </div>
          <div class="tips">
            <span> 商城管理员用户名: mall123</span>
            <span> 商城管理员用户名：mall123</span>
          </div>
          <div class="tips">
            <span> 推广管理员用户名: promotion123</span>
            <span> 推广管理员用户名：promotion123</span>
          </div>
        </div>
      </el-form>
    </div>


    <div class="copyright">
      <!-- Copyright © 2022 xxx.com 版权所有 <a href="https://github.com/linlinjava/litemall">沪ICP备xxx号</a> -->
    </div>

    <el-dialog
      title="注册新用户"
      :visible.sync="showRegist"
      width="40%"
      @close="">
      <div >
        <el-input v-model="userModel.username" placeholder="用户名" size="normal" clearable @change="" style="border: 1px solid #e5e5e5;width: 100%;"></el-input>
        <div style="height: 12px;"></div>
        <el-input v-model="userModel.password" show-password placeholder="密码" size="normal" clearable @change="" style="border: 1px solid #e5e5e5;width: 100%;"></el-input>
        <div style="height: 12px;"></div>
        <el-input v-model="userModel.mobile" placeholder="手机号" size="normal" clearable @change="" style="border: 1px solid #e5e5e5;width: 100%;"></el-input>
      </div>
      <span slot="footer">
        <el-button @click="canselRegistHandle">取消</el-button>
        <el-button type="primary" @click="registHandle">提交</el-button>
      </span>
    </el-dialog>
    


  </div>
</template>

<script>
import { getKaptcha } from '@/api/login'
import LoginApi from './api.js'


export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('管理员密码长度应大于6'))
      } else {
        callback()
      }
    }
    return {
      bgimg:require('@/assets/login-bg.png'),
      loginForm: {
        // username: 'admin123',
        // password: 'admin123',
        username: 'szw',
        // username: 'admin',
        password: '123456',
        code: ''
      },
      codeImg: '',
      loginRules: {
        username: [{ required: true, message: '管理员账户不允许为空', trigger: 'blur' }],
        password: [
          { required: true, message: '管理员密码不允许为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      },
      passwordType: 'password',
      loading: false,
      showRegist:false,
      userModel:{
        username:'',
        password:'',
        mobile:'',
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }

  },
  created() {
    this.getCode()
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    getCode() {
      getKaptcha().then(response => {
        this.codeImg = response.data.data
      })
    },
    /// 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(response => {
            if (response.data.data) {
              this.codeImg = response.data.data
            }
            this.$notify.error({
              title: '失败',
              message: response.data.msg
            })
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    /// 取消注册
    canselRegistHandle(){
      this.showRegist = false;
      this.userModel.username ='';
      this.userModel.password ='';
      this.userModel.mobile ='';
    },
    /// 注册
    async registHandle(){
      if (      
        this.userModel.username ==''||
        this.userModel.password ==''||
        this.userModel.mobile =='') {
          this.$message({
            message: '请填写完整信息后操作',
            type: 'warning',
            showClose: true,
            duration: 3000,
          });
        return;
      }


      let res = await LoginApi.netRegist(this.userModel);
      let data = res['data'];
      let {code,msg} = data;
      let self = this;
      if(code == 200) {
        this.$message({
          message: '注册成功,请登录',
          type: 'success',
          duration: 3000,
          onClose:()=>{
            self.showRegist = false;
          },
        });
      } else {
        this.$message({
          message: msg,
          type: 'error',
          duration: 3000,
        });
        
      }
    },
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    // color: $cursor;
    color: #222222 !important;
  }
}

/* reset element-ui css */
.login-container {
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-content {
  display: flex;
  flex-direction: row;
  margin-left: 40%;
  margin-top: 15%;
  padding: 50px;
  width: 780px;
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 5px 5px 10px 10px rgba(0, 0, 0, 0.1);
}
.login-content-left {
  margin-right: 50px;
  padding-right: 35px;
  width: 340px;
  border-right: 1px solid #ddd;
}
.login-content-left .login-qrcode {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}
.content-qr-text {
  color: #2d3a4b;
}
.content-qrcode {
  margin: 15px 0px;
}
.content-text {
  font-size: 12px;
  color: #aaa;
}
.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 480px;
    max-width: 100%;
    background-color: white;
    // padding: 60px 35px 0;
    // margin: 0 auto;
    overflow: hidden;
    // margin-left: 40%;
    border-radius: 20px;
    // margin-top: 15%;
  }
  .login-code {
    padding-top: 5px;
    float: right;
    img{
      cursor: pointer;
      vertical-align:middle
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      // color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .copyright {
    font-size: 12px;
    color: #fff;
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    margin-bottom: 20px;
    letter-spacing: 0.6px;
    a {
      font-weight: bold;
      border-bottom: 1px solid #fff;
      font-family: "PingFangSC-Semibold", sans-serif;
    }
  }
}
</style>

